import requests
import json
from urllib import parse
import unittest
import time


class WeatherTest(unittest.TestCase):
    def setUp(self):
        # {"深圳": "101280601", "广州": "101280101", "北京": "101010100", "上海": "101020100", "天津": "101030100"}
        self.code = str(input("请输入city_code："))
        self.url = 'http://t.weather.sojson.com/api/weather/city/' + self.code

    def test_weather_shenzhen(self):
        # 深圳：101280601
        # code = str(input("请输入city_code："))
        # url = 'http://t.weather.sojson.com/api/weather/city/101280601'
        r = requests.get(self.url)
        response_data = r.json()
        result = json.dumps(response_data, sort_keys=True, indent=2, ensure_ascii=False)
        response = json.loads(result)
        # print(response.keys())
        # print(response['cityInfo']['city'])
        # print(response['time'])
        # print(response['data']['forecast'][0]['type'])
        # print(response['data']['forecast'][0]['high'])
        # print(response['data']['forecast'][0]['low'])
        # print(response['data']['forecast'][0]['notice'])
        return response['cityInfo']['city'], response['time'], response['data']['forecast'][0]['type'], response['data']['forecast'][0]['high'], response['data']['forecast'][0]['notice']
        # self.assertEqual(result['status'], 200)
        # self.assertEqual(result['message'], 'success感谢又拍云(upyun.com)提供CDN赞助')
        # self.assertEqual(result['cityInfo']['city'], '深圳')

    # def test_weather_param_error(self):
    #
    #     r = requests.get(self.url)
    #     result = r.json()
    #     self.assertEqual(result['message'], 'CityId不在返回之内。')
    #     self.assertEqual(result['status'], 403)
    #
    # def test_weather_param_num_error(self):
    #
    #     r = requests.get(self.url)
    #     result = r.json()
    #     self.assertEqual(result['message'], 'Request resource not found.')
    #     self.assertEqual(result['status'], 404)


if __name__ == '__main__':
    unittest.main()
